<?php
/**
 * 用户模块
 *
 */

class Api_User extends PhalApi_Api {

	public function getRules() {
        return array(
            'getUserInfo' => array(
                'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户状态码'),
            ),
            'register' => array(
                'username' => array('name'=>'username','type'=>'string','require'=>true, 'desc' => '登录名'),
                'password' => array('name'=>'password','type'=>'string','require'=>true, 'desc' => '密码'),
            ),
            'login' => array(
                'username' => array('name'=>'username','type'=>'string','require'=>true, 'desc' => '登录名'),
                'password' => array('name'=>'password','type'=>'string','require'=>true, 'desc' => '密码'),
            ),
			'updateNickname' => array(
                'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户状态码'),
                'nickname' => array('name'=>'nickname','type'=>'string','require'=>true, 'desc' => '昵称'),
            ),
			'logout' => array(
                'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户状态码'),
            ),
        );
	}

	/**
     * 获取用户基本信息
     * @desc 用于获取单个用户基本信息
     * @return int code 操作码，0表示成功， -1表示用户不存在 -2 表示token 失效
     * @return string msg 返回结果信息
     * @return object info 用户信息对象
     * @return int info.username 登录名
     * @return string info.nickname 昵称
     * @return string info.email 邮箱
     * @return string info.register_datetime 注册日期
     */
    public function getUserInfo() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
		if(!$this->checkToken($this->token)){
            return $this->badToken;
        }
		$domain = new Domain_User();
		$info = $domain->getUserInfo($this->userId);
		if (empty($info)) {
			$rs['code'] = -1;
			$rs['msg'] = '获取用户信息失败';
		} else {
			$rs['msg'] = "用户信息";
			$rs['info'] = $info;
		}
		$rs['info'] = $info;
		return $rs;
    }


	/**
	 * [updateNickname 更新昵称]
	 * @return int code 操作码，0表示成功， -1更新昵称失败 -2 表示token 失效
	 * @return string msg 返回结果信息
	 */
	public function updateNickname() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
		if(!$this->checkToken($this->token)){
            return $this->badToken;
        }
		$domain = new Domain_User();
		$rs = $domain->updateNickname($this->userId, $this->nickname);
		return $rs;
    }


    /**
     * 用户注册
     * @desc 用于用户注册
     * @return int code 操作码，0表示成功， -1表示注册失败  -2表示token 失效
     * @return string msg 返回结果信息
     */
    public function register() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $domain = new Domain_User();
        if($domain->checkUsername($this->username)){
            $rs['code'] = -1;
            $rs['msg'] = '用户名已存在';
            return $rs;
        }
        if($domain->register($this->username,$this->password)){
            $rs['msg'] = '注册成功';
        } else {
            $rs['code'] = -1;
            $rs['msg'] = '用户名已存在';
        }
        return $rs;
    }

    /**
     * 用户登录
     * @desc 用户登录
     * @return int code 操作码，0表示成功， -1表示登录失败  -2表示token 失效
     * @return string msg 返回结果信息
     * @return object info 用户信息对象
     * @return int info.token 用户状态码
     */
    public function login()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $domain = new Domain_User();
        $rowUser = $domain->getUserByUsername($this->username);
        if(!$rowUser){
            $rs['code'] = -1;
            $rs['msg'] = '用户名不存在';
            return $rs;
        }
        if($rowUser['data_flag'] == 0 ) {
            $rs['code'] = -1;
            $rs['msg'] = '当前用户已被停用';
            return $rs;
        }
        if($rowUser){
            if($rowUser['password']!=md5($this->password.$rowUser['password_code'])){
                $rs['code'] = -1;
                $rs['msg'] = '用户密码错误';
                return $rs;
            }
            $modelToken = new Model_Token();
            $token = $modelToken->createToken($rowUser['id']);
            if($token == ''){
                $rs['code'] = -1;
                $rs['msg'] = '登录失败,请重试';
            }
            $rs['msg'] = '登录成功';
            $rs['info']['token'] = $token;
            return $rs;
        }
    }


    /** 退出登录
     * @desc 在服务器 不再保存 token
     * @return int code 操作码，0表示成功， -1表示失败  -2表示token 失效
     * @return string msg 返回结果信息
     */
    public function logout()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $modelToken = new Model_Token();
		if(!$this->checkToken($this->token)){
            return $this->badToken;
        }
        if($modelToken->invalidToken($this->token)){
            $rs['msg']='退出成功';
        } else {
            $rs['code'] = -1;
            $rs['msg'] = '退出失败';
        }
        return $rs;
    }
}
